import pandas as pd
import ast
import statistics

fimename = 'test_set_2_3ap'
df = pd.read_csv(fimename + '.csv')

def parse_array_generator(f):
    def ret(value):
        value = str(value)
        if value.isnumeric():
            return value
        elif value.startswith('['):
            a = ast.literal_eval(value)
            return f(a)
        else:
            return ''
    return ret

for colName in df.columns:
    if 'max' in colName:
        print('max', colName)
        df[colName] = df[colName].apply(parse_array_generator(max))
    elif 'sum' in colName:
        print('sum', colName)
        df[colName] = df[colName].apply(parse_array_generator(sum))
    elif 'mean' in colName:
        print('mean', colName)
        df[colName] = df[colName].apply(parse_array_generator(statistics.mean))
    elif 'sta_' in colName:
        print('other', colName)
        df[colName] = df[colName].apply(parse_array_generator(statistics.mean))

moveToCols = ['ap_from_ap_0_max_ant_rssi', 'ap_from_ap_0_mean_ant_rssi', 'ap_from_ap_0_sum_ant_rssi']

def replaceEmptyValue(moveToCols, rawColNamePattern, targetColNamePattern):
    print(moveToCols)
    for index, row in df.iterrows():
        for col in moveToCols:
            if row[col] == '':
                moveCol = col.replace(rawColNamePattern, targetColNamePattern)
                print('replace', row[moveCol])
                df.at[index, col] = row[moveCol]
                df.at[index, moveCol] = ''

replaceEmptyValue(['ap_from_ap_0_max_ant_rssi', 'ap_from_ap_0_mean_ant_rssi', 'ap_from_ap_0_sum_ant_rssi'],
                  'ap_0', 'ap_1')
replaceEmptyValue(['ap_from_ap_1_max_ant_rssi', 'ap_from_ap_1_mean_ant_rssi', 'ap_from_ap_1_sum_ant_rssi'],
                  'ap_1', 'ap_2')

df.to_csv(fimename + '_ReduceRssi.csv')
